package com.faf.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.faf.bean.Likes;
import com.faf.dao.LikesDao;

public class LikesDaoImpl extends BaseDaoImpl implements LikesDao
{

	@Override
	public List<Likes> getLikesListById(int userId) throws SQLException
	{
		String sql = "select likeid, userid, foodid, likeremark from likes where userid=?";
		executeQuery(sql,userId);
		List<Likes> likesList = new ArrayList<>();
		while(rs.next()) {
			Likes likes = new Likes(rs.getInt("likeid"),rs.getInt("userid"),rs.getInt("foodid"),rs.getString("likeremark"));
			likesList.add(likes);
		}
		close();
		super.con = null;
		return likesList;
	}

	@Override
	public long getLikeNumberByFoodId(int foodId) throws SQLException {
		// TODO Auto-generated method stub
		String str_sql = "select count(*) from likes where foodid = ?";
		this.executeQuery(str_sql, foodId);
		if(rs.next())
			return rs.getLong(1);
		return 0;
	}

	@Override
	public List<Likes> getLikesList() throws SQLException
	{
		String sql = "select likeid, userid, foodid, likeremark from likes";
		executeQuery(sql);
		List<Likes> likesList = new ArrayList<>();
		while(rs.next()) {
			Likes likes = new Likes(rs.getInt("likeid"),rs.getInt("userid"),rs.getInt("foodid"),rs.getString("likeremark"));
			likesList.add(likes);
		}
		close();
		super.con = null;
		return likesList;
	}



	@Override
	public void newLikeInsert(int userId, int foodId) throws SQLException {
		// TODO Auto-generated method stub
		if(this.isUserLike(userId, foodId))
			return;
		String str_sql = "insert into likes (userid, foodid) values (?, ?)";
		this.executeUpdate(str_sql, userId, foodId);
	}

	@Override
	public void newLikeDelete(int userId, int foodId) throws SQLException {
		// TODO Auto-generated method stub
		if(!this.isUserLike(userId, foodId))
			return;
		String str_sql = "delete from likes where userid = ? and foodid = ?";
		this.executeUpdate(str_sql, userId, foodId);
	}

	@Override
	public boolean isUserLike(int userId, int foodId) throws SQLException {
		// TODO Auto-generated method stub
		String str_sql = "select * from likes where userid = ? and foodid = ?";
		this.executeQuery(str_sql, userId, foodId);
		if(rs.next())
			return true;
		return false;
	}
}
